﻿using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfance;
using B.S.BasicData.Write.Application.Common.Role;
using MediatR;

namespace B.S.BasicData.Write.Application.Handler.Role
{
    public class DelRoleaccountnameHandler : IRequestHandler<DelRoleaccountnameCommon, ApiResult<int>>
    {
        private readonly IBaseRepository<AccountRoleModel> accountroleRepo;

        public DelRoleaccountnameHandler(IBaseRepository<AccountRoleModel> accountroleRepo)
        {
            this.accountroleRepo = accountroleRepo;
        }
        public async Task<ApiResult<int>> Handle(DelRoleaccountnameCommon request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();

            var info= accountroleRepo.GetAll().Where(x=>x.UserId==request.UserId&&x.RoleId==request.RoleId).FirstOrDefault();
            var account = await accountroleRepo.Del(info);

            if(account>0)
            {
                res.Data = account;
                res.Code=ApiEnum.Success;
                res.Message = "删除成功";
                return await Task.FromResult(res);
            }
            else
            {
                res.Data = account;
                res.Code = ApiEnum.Error;
                res.Message = "删除失败";
                return await Task.FromResult(res);
            }


        }
    }
}
